import { defineComponent, ref, computed } from "vue";
import PageTitle from "@/components/PageTitle";
import Form from "@/components/Form";
import { IFormItem } from "@/components/Form/types";
import ButtonView from "@/components/ButtonView";
import CardView from "@/components/CardView";
import { useRequest } from "./request";
import BannerUploader from "@/components/BannerUploader";
import FormGroup from "@/components/FormGroup";
import { FormGroupPropsData } from "@/components/FormGroup/types";
import * as vant from "vant";
const { showFailToast } = vant;
export default defineComponent({
  name: "",
  emits: ["back", "submit"],
  setup(_, { emit }) {
    const form = ref({
      img: "",
      name: "",
    });
    const { confirm } = useRequest();
    const data = computed<FormGroupPropsData[]>(() => [
      {
        data: [
          {
            label: "仪器名称",
            type: "input",
            key: "name",
            rules: [
              {
                required: true,
              },
            ],
          },
        ],
      },
      {
        title: "",
        data: [
          {
            key: "remark",
            label: "农资介绍",
            type: "textarea",
          },
        ],
      },
      {
        title: "",
        data: [
          {
            key: "features",
            label: "设备特点",
            type: "textarea",
          },
        ],
      },
    ]);
    const render1 = () => {
      return (
        <BannerUploader
          uploadText="上传设备照片"
          v-model={form.value.img}
          class="w-full !h-[178px]"
        />
      );
    };
    const render2 = () => {
      return (
        <div class={"p-[10px]"}>
          <FormGroup
            data={data.value}
            v-model={form.value}
            class="m-child-top-20"
          />
          <ButtonView
            block
            type="primary"
            onClick={() => {
              if (!form.value.img) {
                showFailToast("请上传仪器图片");
                return;
              }
              if (!form.value.name) {
                showFailToast("请输入仪器名称");
                return;
              }
              emit("submit", form.value);
            }}
            class="!mt-[20px] !h-[40px]"
          >
            确定
          </ButtonView>
        </div>
      );
    };
    return () => {
      return (
        <PageTitle title="设备详情" onBack={() => emit("back")}>
          {render1()}
          {render2()}
        </PageTitle>
      );
    };
  },
});
